package com.tjsinfo.tjsoa.useseal.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.useseal.bo.ITUseSealSealBO;
import com.tjsinfo.tjsoa.useseal.vo.TUseSealSeal;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;

public class SealListAction extends Action {
	private ITUseSealSealBO sealBo;

	public void setSealBo(ITUseSealSealBO sealBo) {
		this.sealBo = sealBo;
	}
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<TUseSealSeal> list = null;
		Integer start = 0;
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		String columnName = request.getParameter("sealName");
		Integer sealMark = Integer.parseInt(request.getParameter("sealMark"));
		String node = request.getParameter("node");
		
		if (columnName == null || columnName == "") {
			if(request.getParameter("start") == null)
			{
				list = sealBo.selectAll(0, 10,sealMark,request);
			}
			else
			{
				start = Integer.parseInt(request.getParameter("start"));
				list = sealBo.selectAll(start, 10,sealMark,request);
			}
		} else {
			if(request.getParameter("start") == null)
			{
				list = sealBo.selectColumn(0, 10,sealMark, "sealName", columnName, request);
			}
			else
			{
				start = Integer.parseInt(request.getParameter("start"));
				list = sealBo.selectColumn(start, 10,sealMark, "sealName", columnName, request);
			}
		}
		
		for (TUseSealSeal gi : list) {
			if("退回".equals(gi.getStatus())){
				WorkFlowTemplate test = new WorkFlowTemplate();
				List list2 = new ArrayList();
				Integer userId = ToolKit.getSessionUser(request).getUser_id();
				List list1 = test.selectTask(userId.toString(), node);
				for (Iterator iterator = list1.iterator(); iterator.hasNext();) {
					TaskInstance taskInstance = (TaskInstance) iterator.next();
					Long flowid =	taskInstance.getProcessInstance().getId();
					String flowid1 = gi.getFlowid().toString();
					if(flowid.toString().equals(flowid1)){
						gi.setTaskid(taskInstance.getId());
					}
				}
			}
			gi.setDateTime1(sdf.format(gi.getDateTime()));
			JSONObject giJson = new JSONObject(gi);
			array.put(giJson);
		}
		
		object.put("totalProperty",sealBo.selectTotal());
		object.put("root", array);
		response.getWriter().print(object);
		return null;
	}
}
